//
//  XMSearchScreenView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/16.
//

#import "XMSearchScreenView.h"

@interface XMSearchScreenView ()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) NSMutableArray *itemBtnArr;
@property (nonatomic, strong) NSMutableArray *userItemBtnArr;

@property (nonatomic, assign) BOOL isUser;

@end

@implementation XMSearchScreenView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.contentView];
    self.backgroundColor = kColorHEXA(0x464646, 0.46);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
    self.alpha = 0;
}

- (void)showWithScreenDic:(NSDictionary *)screenDic
                   isUser:(BOOL)isUser {
    for (UIView *subView in self.contentView.subviews) {
        [subView removeFromSuperview];
    }
    _isUser = isUser;
    NSMutableArray *currentBtnArr;
    if (_isUser) {
        currentBtnArr = self.userItemBtnArr;
    }
    else {
        currentBtnArr = self.itemBtnArr;
    }
    if (currentBtnArr.count > 0) {
        [self retSetContentSubViewWithScreenDic:screenDic itemBtnArr:currentBtnArr];
        return;
    }
    
    NSArray *keyArr = [screenDic allKeys];
    CGFloat itemWidth = (self.width - KScaleWidth(20.0)*3)/3;
    CGFloat frameY = KScaleWidth(10.0);
    for (int i = keyArr.count - 1; i >= 0; i --) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(8.0), frameY, self.width, KScaleWidth(26.0)) font:KFontSize(16.0) textColor:kColorHEX(0x666666)];
        titleLabel.text = keyArr[i];
        [_contentView addSubview:titleLabel];
        
        NSArray *itemTitleArr = [screenDic safeArrayForKey:keyArr[i]];
        NSMutableArray *itemBtnArr = [NSMutableArray array];
        for (int j = 0; j < itemTitleArr.count; j ++) {
            UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(10.0) + (itemWidth + KScaleWidth(20.0))*(j%3), titleLabel.maxY + KScaleWidth(10.0) + (KScaleWidth(34.0) + KScaleWidth(20.0))*(j/3), itemWidth, KScaleWidth(34.0)) title:itemTitleArr[j] selectTitle:itemTitleArr[j] titleColor:kColorHEX(0x666666) selectColor:kColorHEX(0x333333)];
            [itemBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
            [itemBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xFFD800)] forState:UIControlStateSelected];
            [itemBtn addTarget:self action:@selector(itemSelectAction:) forControlEvents:UIControlEventTouchUpInside];
            [itemBtn setCornerRadius:KScaleWidth(8.0)];
            [itemBtnArr addObject:itemBtn];
            [_contentView addSubview:itemBtn];
            if (j == 0) {
                itemBtn.selected = YES;
            }
        }
        frameY += (itemTitleArr.count/3 + (itemTitleArr.count%3 > 0 ? 1 : 0))*(KScaleWidth(34.0) + KScaleWidth(20.0)) + titleLabel.height + KScaleWidth(10.0);
        if (isUser) {
            [self.userItemBtnArr addObject:itemBtnArr];
        }
        else {
            [self.itemBtnArr addObject:itemBtnArr];
        }
    }
    
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake((self.width - KScaleWidth(90.0)*2 - KScaleWidth(40.0))/2, frameY + KScaleWidth(20.0), KScaleWidth(90.0), KScaleWidth(32.0)) title:@"重置" titleColor:kColorHEX(0x666666)];
    resetBtn.backgroundColor = kColorHEX(0xF1F1F1);
    [resetBtn setCornerRadius:resetBtn.height/2];
    [resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:resetBtn];
    
    UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(resetBtn.maxX + KScaleWidth(40.0), resetBtn.y, resetBtn.width, resetBtn.height) title:@"完成" titleColor:kColorHEX(0x333333)];
    [completeBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [completeBtn addTarget:self action:@selector(completeAction:) forControlEvents:UIControlEventTouchUpInside];
    [completeBtn setCornerRadius:completeBtn.height/2];
    [_contentView addSubview:completeBtn];
//    _contentView.height = completeBtn.maxY + KScaleWidth(25.0);
    
    
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.contentView.height = frameY + KScaleWidth(20.0) + KScaleWidth(32.0) + KScaleWidth(25.0);
        weakSelf.contentView.backgroundColor = kColorHEX(0xffffff);
        [weakSelf.contentView setPartCornerWithRectCorner:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadius:KScaleWidth(12.0)];
        weakSelf.alpha = 1;
    }];
}

- (void)retSetContentSubViewWithScreenDic:(NSDictionary *)screenDic
                               itemBtnArr:(NSArray *)currentBtnArr  {
    NSArray *keyArr = [screenDic allKeys];
    CGFloat frameY = KScaleWidth(10.0);
    for (int i = 0; i < keyArr.count; i ++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(8.0), frameY, self.width, KScaleWidth(26.0)) font:KFontSize(16.0) textColor:kColorHEX(0x666666)];
        titleLabel.text = keyArr[i];
        [_contentView addSubview:titleLabel];
        
        NSMutableArray *itemBtnArr = currentBtnArr[i];
        for (int j = 0; j < itemBtnArr.count; j ++) {
            UIButton *itemBtn = itemBtnArr[j];
            [_contentView addSubview:itemBtn];
        }
//        frameY += (itemBtnArr.count/3 + (itemBtnArr.count%3 > 0 ? 1 : 0))*(KScaleWidth(34.0) + KScaleWidth(20.0));
        frameY += (itemBtnArr.count/3 + (itemBtnArr.count%3 > 0 ? 1 : 0))*(KScaleWidth(34.0) + KScaleWidth(20.0)) + titleLabel.height + KScaleWidth(10.0);
    }
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake((self.width - KScaleWidth(90.0)*2 - KScaleWidth(40.0))/2, frameY + KScaleWidth(20.0), KScaleWidth(90.0), KScaleWidth(32.0)) title:@"重置" titleColor:kColorHEX(0x666666)];
    resetBtn.backgroundColor = kColorHEX(0xF1F1F1);
    [resetBtn setCornerRadius:resetBtn.height/2];
    [resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:resetBtn];
    
    UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(resetBtn.maxX + KScaleWidth(40.0), resetBtn.y, resetBtn.width, resetBtn.height) title:@"完成" titleColor:kColorHEX(0x333333)];
    [completeBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [completeBtn addTarget:self action:@selector(completeAction:) forControlEvents:UIControlEventTouchUpInside];
    [completeBtn setCornerRadius:completeBtn.height/2];
    [_contentView addSubview:completeBtn];
//    _contentView.height = completeBtn.maxY + KScaleWidth(25.0);
//    [_contentView setPartCornerWithRectCorner:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadius:KScaleWidth(12.0)];
    
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.contentView.height = frameY + KScaleWidth(20.0) + KScaleWidth(32.0) + KScaleWidth(25.0);
        weakSelf.contentView.backgroundColor = kColorHEX(0xffffff);
        [weakSelf.contentView setPartCornerWithRectCorner:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadius:KScaleWidth(12.0)];
        weakSelf.alpha = 1;
    }];
}

- (void)show {
    XMWeakSelf;
    [UIView  animateWithDuration:.3 animations:^{
        weakSelf.alpha = 1;
    }];
}

- (void)itemSelectAction:(UIButton *)button {
    NSMutableArray *selectArr;
    if (_isUser) {
        selectArr = self.userItemBtnArr;
    }
    else {
        selectArr = self.itemBtnArr;
    }
    for (NSArray *itemBtnArr in selectArr) {
        if ([itemBtnArr containsObject:button]) {
            for (UIButton *btn in itemBtnArr) {
                btn.selected = NO;
            }
        }
    }
    button.selected = YES;
}

//完成
- (void)completeAction:(UIButton *)button {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.contentView.height = 1;
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];

    if (self.isUser) {
        NSArray *itemBtnArr = [self.itemBtnArr firstObject];
        NSString *key = @"";
        for (int i = 0; i < itemBtnArr.count; i ++) {
            UIButton *itemBtn = itemBtnArr[i];
            if (itemBtn.selected) {
                key = @[@"1", @"2", @"3", @"4", @"5", @"6"][i];
            }
        }
        self.screenBlock ? self.screenBlock(@"", @"") : nil;
    }
    else {
        NSString *keyOne = @"";
        NSString *keyTwo = @"";
        for (int i = 0; i < self.itemBtnArr.count; i ++) {
            NSArray *itemBtnArr = self.itemBtnArr[i];
            for (int j = 0; j < itemBtnArr.count; j ++) {
                UIButton *itemBtn = itemBtnArr[j];
                if (itemBtn.selected) {
                    if (i == 0) {
                        keyOne = @[@"", @"1", @""][j];
                    }
                    else {
                        keyTwo = @[@"1", @"2", @"3", @"4"][j];
                    }
                }
            }
        }
        self.screenBlock ? self.screenBlock(keyOne, keyTwo) : nil;
    }
}

//重置
- (void)resetAction:(UIButton *)button {
    for (NSArray *itemBtnArr in self.itemBtnArr) {
        for (UIButton *itemBtn in itemBtnArr) {
            itemBtn.selected = NO;
        }
        UIButton *itemBtn = [itemBtnArr firstObject];
        itemBtn.selected = YES;
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.contentView.height = 1;
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - Init
- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 10)];
        _contentView.backgroundColor = kColorHEX(0xffffff);
    }
    return _contentView;
}

- (NSMutableArray *)itemBtnArr {
    if (!_itemBtnArr) {
        _itemBtnArr = [NSMutableArray array];
    }
    return _itemBtnArr;
}

- (NSMutableArray *)userItemBtnArr {
    if (!_userItemBtnArr) {
        _userItemBtnArr = [NSMutableArray array];
    }
    return _userItemBtnArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
